// IniFile.cpp: implementation of the CIniFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniFile.h"
#include "Function.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniFile::CIniFile()
{
	m_lpszFileNme=NULL;
}
CIniFile::CIniFile(LPCTSTR lpszfileName)
{	
	m_lpszFileNme = NULL;	
	if(lpszfileName != NULL)
	{
		Safestrcpy(m_szFileName, MAX_PATH, lpszfileName);
		m_lpszFileNme = (LPTSTR)m_szFileName;
	}
}

CIniFile::~CIniFile()
{

}

int CIniFile::Get_int(LPCTSTR lpAppName, LPCTSTR lpKeyName, int nDefault)
{
	if(m_lpszFileNme==NULL)
	{
		return nDefault;
	}
	return GetPrivateProfileInt(lpAppName, lpKeyName, nDefault, m_lpszFileNme);
}

LPTSTR CIniFile::Get_str(LPCTSTR lpAppName,LPCTSTR  lpKeyName,LPCTSTR  lpDefault)
{
	static char szValue[1024]={0};
	DWORD nSize = 1024;
	if(m_lpszFileNme == NULL)
	{
		return NULL;
	}	
	GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, (LPTSTR)szValue, nSize, m_lpszFileNme);
	return (LPTSTR)szValue;
}

void CIniFile::Write_str(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString)
{
	if(m_lpszFileNme == NULL)
	{
		return ;
	}
	WritePrivateProfileString(lpAppName, lpKeyName, lpString, m_lpszFileNme);
}

void CIniFile::Write_int(LPCTSTR lpAppName, LPCTSTR lpKeyName, int nKey)
{
	if(m_lpszFileNme == NULL)
	{
		return ;
	}
	CString str;
	str.Format (_T("%d"), nKey);
	WritePrivateProfileString(lpAppName, lpKeyName, str, m_lpszFileNme);
}

void CIniFile::SetFileName(LPCTSTR lpszFileName)
{
	if(lpszFileName !=NULL)
	{
		Safestrcpy(m_szFileName, MAX_PATH, lpszFileName);
		m_lpszFileNme = (LPTSTR)m_szFileName;
	}
}

LPTSTR CIniFile::GetFileName()
{
	Safestrcpy(m_szFileName, MAX_PATH, m_lpszFileNme);
	return (LPTSTR)m_szFileName;
}
